from config.configs import DATA_PATH, DOWNLOAD_PATH
import os
import pandas as pd
from tools.funciton import write_xlsx, read_xlsx_all, read_xlsx_file, update_colum_datas


def check_stock_difference(yc_file_name, jw_file_name, name):
    """云仓集物库存差异校验"""
    file1 = os.path.join(DOWNLOAD_PATH, yc_file_name)
    file2 = os.path.join(DATA_PATH, jw_file_name)
    yun_spus = read_xlsx_file(file1, 8)
    li = [i.strip() for i in yun_spus]
    update_colum_datas(file1, '货号', li)
    yun_data = read_xlsx_all(file1).rename(columns={'可用数量': '总库存'})
    jw_data = read_xlsx_all(file2)
    yun_datas = yun_data.pivot_table(values='总数量', index='货号', columns=None, aggfunc='sum').to_dict('index')
    jw_datas = jw_data.pivot_table(values='库存数量', index='货号', columns=None, aggfunc='sum').to_dict('index')
    yun_dic = {k: v['总数量'] for k, v in yun_datas.items()}
    jw_dic = {k: v['库存数量'] for k, v in jw_datas.items()}
    yun = list(yun_dic.keys())
    jw = list(jw_dic.keys())
    li = []
    for d in range(len(yun)):
        if yun[d] not in jw_dic:
            li.append([yun[d], yun_dic[yun[d]], yun[d], 0])
        elif yun_dic[yun[d]] != jw_dic[yun[d]]:
            li.append([yun[d], yun_dic[yun[d]], yun[d], jw_dic[yun[d]]])
    for j in range(len(jw)):
        if jw[j] not in yun_dic:
            li.append([jw[j], 0, jw[j], jw_dic[jw[j]]])
    write_xlsx(['云仓货号', '云仓库存数', '集物货号', '集物库存数'], li, name)


check_stock_difference('8.27库存(1).xlsx', '极速仓0828总库存.xlsx', '0828差异')


